/**
 * Project: greenline-prepose-client-biz-dal
 * 
 * File Created at 2013-12-18
 * 
 * Copyright 2012 Greenline.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Greenline Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Greenline.com.
 */
package org.xukai.dal.datasource;

import com.google.common.collect.Maps;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.xukai.dal.base.BaseDAO;
import org.xukai.dal.mapper.GroupMapper;

import java.util.HashMap;


/**
 * @Type DynamicFactory
 * @Desc 动态注入db配置的工厂类
 * @author jianyun.zheng
 * @date 2013-12-18
 * @Version V1.0
 */
public class DynamicFactory implements ApplicationContextAware {
    private final Log logger = LogFactory.getLog(getClass());
    private String dynamicPath;
    private ConfigurableApplicationContext applicationContext;


    /**
     * * @desc 初始化根据配置文件得到的bean
     * 
     */
    public void initSpingDal() {
        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) applicationContext
                .getBeanFactory();
        logger.info("register datasource");
        // 添加datasource配置
        BeanDefinitionBuilder datasource = BeanDefinitionBuilder.genericBeanDefinition(DynamicDataSource.class);
        datasource.addPropertyValue("targetDataSources", new HashMap<String,DatasourceData>());
        defaultListableBeanFactory.registerBeanDefinition("dataSource", datasource.getBeanDefinition());

        logger.info("register mysqlMapClient");
        // 添加mysqlMapClient bean配置
        BeanDefinitionBuilder mysqlMapClient = BeanDefinitionBuilder.genericBeanDefinition(SqlSessionFactoryBean.class);
        mysqlMapClient.addPropertyValue("configLocation", "file:" + dynamicPath + "sqlMapConfig.xml");
        mysqlMapClient.addPropertyReference("dataSource", "dataSource");
        defaultListableBeanFactory.registerBeanDefinition("mysqlMapClient", mysqlMapClient.getBeanDefinition());

        logger.info("register baseDAO");
        // 添加baseDAO配置
        BeanDefinitionBuilder baseDAO = BeanDefinitionBuilder.genericBeanDefinition(BaseDAO.class);
        baseDAO.addPropertyReference("sqlMapClient", "mysqlMapClient");
        defaultListableBeanFactory.registerBeanDefinition("baseDAO", baseDAO.getBeanDefinition());

        logger.info("register dBOperate");
        // 添加dBOperate配置
        BeanDefinitionBuilder dBOperate = BeanDefinitionBuilder.genericBeanDefinition(GroupMapper.class);
        dBOperate.setParentName("baseDAO");
        defaultListableBeanFactory.registerBeanDefinition("dBOperate", dBOperate.getBeanDefinition());

    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = (ConfigurableApplicationContext) applicationContext;

    }

    public ConfigurableApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @SuppressWarnings("unchecked")
    public <T> T getBean(String bean, Class<T> clazz) {
        return (T) applicationContext.getBean(bean);
    }

    public Object getBean(String bean) {
        return applicationContext.getBean(bean);
    }

    public <T> Object getBean(Class<T> clazz) {
        return applicationContext.getBean(clazz);
    }

}
